package com.zhongjinkang.rhythm.AppView.Dialog

import android.app.Dialog
import android.content.DialogInterface
import android.graphics.Color
import android.graphics.drawable.ColorDrawable
import android.os.Bundle
import android.util.DisplayMetrics
import android.view.Gravity
import android.view.KeyEvent
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.view.WindowManager
import androidx.annotation.Nullable
import androidx.fragment.app.DialogFragment
import androidx.fragment.app.FragmentManager
import com.zhongjinkang.rhythm.R


open abstract class BaseDialog: DialogFragment() {
    private var isCancelable = true
    private var isShow=false





    override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
        val dialog=Dialog(requireContext(), R.style.MyDialog)
        dialog.setCancelable(isCancelable)
        dialog.setCanceledOnTouchOutside(isCancelable)
        dialog.setOnKeyListener(object : DialogInterface.OnKeyListener {
            override fun onKey(dialog: DialogInterface?, keyCode: Int, event: KeyEvent?): Boolean {
              return keyCode==KeyEvent.KEYCODE_BACK
            }

        })
        return dialog
    }


    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        dialog?.let { it ->
            it.window?.let {it1->
                it1.setBackgroundDrawable(ColorDrawable(Color.TRANSPARENT))
                   val params: WindowManager.LayoutParams = it1.attributes
                   params.gravity = Gravity.CENTER
                   params.width = WindowManager.LayoutParams.MATCH_PARENT
                   params.height = WindowManager.LayoutParams.MATCH_PARENT
                   params.dimAmount = 0.5f
                   it1.attributes=params
                   val dm = DisplayMetrics()
                   activity?.let {it2->
                       it2.windowManager.defaultDisplay.getMetrics(dm)
                       it1.setLayout((dm.widthPixels*0.8).toInt(),ViewGroup.LayoutParams.WRAP_CONTENT)
                   }



           }

        }

        return super.onCreateView(inflater, container, savedInstanceState)
    }


    override fun onStart() {
        super.onStart()
        val dm = DisplayMetrics()
        dialog?.let {

            activity?.let {it1->
              it1.windowManager.defaultDisplay.getMetrics(dm)

            }
            if (activity != null) {
                activity!!.windowManager.defaultDisplay.getMetrics(dm)
                if (dialog!!.window != null) dialog!!.window!!.setLayout(
                    (dm.widthPixels * 0.8).toInt(),
                    ViewGroup.LayoutParams.WRAP_CONTENT
                )
//                        dialog.getWindow().setLayout( ViewGroup.LayoutParams.MATCH_PARENT,
//                                ViewGroup.LayoutParams.MATCH_PARENT);
            }

        }

    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
    }


    override fun dismiss() {
        super.dismiss()
        hideDialog()
        isShow=false;
    }


    override fun dismissAllowingStateLoss() {
        super.dismissAllowingStateLoss()
        hideDialog()
        isShow=false
    }
    override fun show(manager: FragmentManager, @Nullable tag: String?) {
        if (!isShow) {
            try {
                isShow = true

                if (!this.isAdded) manager.beginTransaction().add(this, tag)
                    .commitAllowingStateLoss()
                showDialog()
            } catch (e: Exception) {
                e.printStackTrace()
            }
        }
    }



    fun show(manager: FragmentManager) {
        show(manager, this.tag)
    }

    fun isShow(): Boolean {
        return isShow
    }


    abstract fun showDialog();

    abstract fun hideDialog();
}